package org.corey.share.business;

import java.io.InputStream;
import java.util.Date;

import org.apache.struts2.ServletActionContext;
import org.apache.tools.ant.filters.StringInputStream;
import org.corey.share.db.entity.Comment;
import org.corey.share.db.service.CommentService;
import org.corey.share.db.service.CommentServiceImpl;
import org.corey.share.misc.Common;

import com.opensymphony.xwork2.ActionSupport;

public class CommentAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2652110969455182134L;
	private CommentService service = new CommentServiceImpl();

	private Comment comment;
	private Long articleId;
	private boolean result;
	
	public InputStream getInputStream(){
		String s="false";
		if(result){
			s="{result:true,id:'"+comment.getKey().getId()+"',name:'"+this.comment.getName()+"',writeDate:'"+Common.parseDate(comment.getWriteDate())+"',ip:'"+comment.getIp()+"',content:'"+comment.getContent()+"'}";
		}
		StringInputStream inputStream=new StringInputStream(s);
		return inputStream;
	}
	
	

	public Comment getComment() {
		return comment;
	}



	public void setComment(Comment comment) {
		this.comment = comment;
	}



	public Long getArticleId() {
		return articleId;
	}



	public void setArticleId(Long articleId) {
		this.articleId = articleId;
	}



	public String execute() {
		result = false;
		if (comment != null && articleId != null) {
			comment.setWriteDate(new Date());
			comment.setIp(ServletActionContext.getRequest().getRemoteAddr());
			result = service.saveComment(articleId, comment);
		}
		result=true;
		return SUCCESS;
	}
}
